﻿using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using IFDB.Models.Contexts;
using IFDB.ResourceLibrary;
using System.Collections.Generic;
using IFDB.Models.Foundation;

namespace IFDB.Models.Tables
{
    public class AppearanceEvent : BaseTable<AppearanceEvent>
    {
        [Key]
        public Guid AppearanceEventId { get; set; }

        public Guid MatchId { get; set; }
        public Guid PersonId { get; set; }
        public Guid TeamId { get; set; }
        public MatchEventType MatchEventType { get; set; }

        public int? Minute { get; set; }
        public int? Plus { get; set; }

        [ForeignKey("MatchId")]
        public virtual Match Match { get; set; }

        [ForeignKey("PersonId")]
        public virtual Person Person { get; set; }

        [ForeignKey("TeamId")]
        public virtual Team Team { get; set; }

        public static AppearanceEvent CreateNew(Guid matchId, Guid personId, Guid teamId, MatchEventType matchEventType, int? minute, int? plus)
        {
            return new AppearanceEvent()
            {
                AppearanceEventId = Guid.NewGuid(),
                MatchId = matchId,
                PersonId = personId,
                TeamId = teamId,
                MatchEventType = matchEventType,
                Minute = minute,
                Plus = plus
            };
        }

        public static AppearanceEvent Update(AppearanceEvent appearanceEvent, Guid matchId, Guid personId, Guid teamId, MatchEventType matchEventType, int? minute, int? plus)
        {
            appearanceEvent.MatchId = matchId;
            appearanceEvent.PersonId = personId;
            appearanceEvent.TeamId = teamId;
            appearanceEvent.MatchEventType = matchEventType;
            appearanceEvent.Minute = minute;
            appearanceEvent.Plus = plus;

            return appearanceEvent;
        }

        public static AppearanceEvent Search(IFDBContext db, Guid matchId, Guid personId, Guid teamId, MatchEventType matchEventType)
        {
            var localQuery = from appearanceEvent in db.AppearanceEvents.Local
                             where appearanceEvent.MatchId == matchId
                             && appearanceEvent.PersonId == personId
                             && appearanceEvent.TeamId == teamId
                             && appearanceEvent.MatchEventType == matchEventType
                             select appearanceEvent;

            var query = from appearanceEvent in db.AppearanceEvents
                        where appearanceEvent.MatchId == matchId
                        && appearanceEvent.PersonId == personId
                        && appearanceEvent.TeamId == teamId
                        && appearanceEvent.MatchEventType == matchEventType
                        select appearanceEvent;

            var search = ProcessSearch(localQuery, query, false);
            return search != null ? search.Single() : null;
        }
    }
}
